local manjuan = fk.CreateSkill({
  name = "sj3__manjuan",
  tags = { Skill.Switch },
})

Fk:loadTranslationTable {
  ["sj3__manjuan"] = "漫卷",
  [":sj3__manjuan"] = "<b>转换技</b>，出牌阶段，你可以重铸任意个区域内所有牌，且：阳：至少三张；阴：至多两张。",

  ["#sj3__manjuan"] = "漫卷：你可以重铸任意个区域内所有牌（%arg）",
  ["#sj3__manjuan_3"] = "至少三张",
  ["#sj3__manjuan_2"] = "至多两张",

  ["$sj3__manjuan1"] = "漫卷纵酒，白首狂歌。",
  ["$sj3__manjuan2"] = "吾有雄才，漫天卷地。",
}

local cwh_util = require "packages.sanshe.pkg.utility.cwh_util"

local area_table = {
  [Player.Hand] = "$Hand",
  [Player.Equip] = "$Equip",
  [Player.Judge] = "$Judge",
}

manjuan:addEffect("active", {
  anim_type = "switch",
  prompt = function(self, player, selected_cards, selected_targets)
    return "#sj3__manjuan:::" ..
    ((player:getSwitchSkillState("sj3__manjuan", false) == fk.SwitchYang) and "#sj3__manjuan_3" or "#sj3__manjuan_2")
  end,
  interaction = function(self, player)
    local all, ask = {}, {}
    for i, v in ipairs(area_table) do
      table.insert(all, v)
      if #player:getCardIds(i) > 0 then
        table.insert(ask, v)
      end
    end
    return cwh_util.CheckBox {
      choices = ask,
      all_choices = all,
      max_num = 3,
    }
  end,
  card_filter = Util.FalseFunc,
  target_filter = Util.FalseFunc,
  feasible = function(self, player, selected, selected_cards, card)
    if self.interaction.data and #self.interaction.data > 0 then
      local num = 0
      for i, v in ipairs(area_table) do
        if table.contains(self.interaction.data, v) then
          num = num + #player:getCardIds(i)
        end
      end
      if (player:getSwitchSkillState("sj3__manjuan", false) == fk.SwitchYang) then
        return num > 2
      else
        return num < 3
      end
    end
    return false
  end,
  can_use = function(self, player)
    return not player:isAllNude()
  end,
  on_use = function(self, room, effect)
    local player = effect.from
    local cards = {}
    for i, v in ipairs(area_table) do
      if table.contains(self.interaction.data, v) then
        table.forEach(player:getCardIds(i), function(id)
          table.insert(cards, id)
        end)
      end
    end
    room:recastCard(cards, player, manjuan.name)
  end,
})

return manjuan
